import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/request/login_request.dart';
import 'package:flutter_app_unionad/ui/basice/base_navigation_widget.dart';
import 'package:flutter_app_unionad/ui/basice/my_button.dart';
import 'package:flutter_app_unionad/ui/login/widget/login_input_widget.dart';

//修改手机号
class ChangeMobilePage extends StatefulWidget {
  static const String routeName = "/changeMobilePage";

  @override
  _ChangeMobilePageState createState() => _ChangeMobilePageState();
}

class _ChangeMobilePageState extends State<ChangeMobilePage> {
  String? oldMobile;
  String? newMobile;
  String? code;

  @override
  Widget build(BuildContext context) {
    return BaseNavigationWidget(
        title: "修改手机号",
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.symmetric(horizontal: 25.px, vertical: 40.px),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10.px),
              ),
              child: buildContent(),
            ),
            MySizeBox(height: 30),
            MyButton(
              title: "确认",
              onTap: () {
                saveOnTap();
              },
            ),
          ],
        ));
  }

  Widget buildContent() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "原手机号",
          style: TextStyle(
            color: MyColors.color_27_27_27,
            fontWeight: MyFontWeight.bold,
            fontSize: MyFont.font14,
          ),
        ),
        MySizeBox(height: 10),
        Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.px),
            child: ShowTitleDetailListWidget(
              title: UserCacheUil.getString(Configuration.mobile) == null ||
                      UserCacheUil.getString(Configuration.mobile)!.length == 0
                  ? ''
                  : UserCacheUil.getString(Configuration.mobile)!.replaceRange(3, 7, " **** "),
              titleStyle: TextStyle(
                color: MyColors.formHex("#272727FF"),
                fontSize: MyFont.font12,
              ),
              padding: EdgeInsets.symmetric(horizontal: 0, vertical: SizeFill.setRx(5)),
              showBottomLine: false,
            )
            // LoginInputWidget(
            //   leftIconWidget: SizedBox(width: 5),
            //   placeholder: "请输入原手机号",
            //   inputTextTapChange: (value) => oldMobile = value,
            //   keyboardType: TextInputType.number,
            // ),
            ),
        MySizeBox(height: 10),
        Text(
          "输入新的手机号",
          style: TextStyle(
            color: MyColors.color_27_27_27,
            fontWeight: MyFontWeight.bold,
            fontSize: MyFont.font14,
          ),
        ),
        MySizeBox(height: 10),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 10.px),
          child: LoginInputWidget(
            leftIconWidget: SizedBox(width: 5),
            placeholder: "请输入新的手机号",
            inputTextTapChange: (value) => newMobile = value,
            keyboardType: TextInputType.number,
          ),
        ),
        MySizeBox(height: 10),
        Text(
          "验证码",
          style: TextStyle(
            color: MyColors.color_27_27_27,
            fontWeight: MyFontWeight.bold,
            fontSize: MyFont.font14,
          ),
        ),
        MySizeBox(height: 10),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 10.px),
          child: Row(
            children: [
              Expanded(
                child: LoginInputWidget(
                  leftIconWidget: SizedBox(width: 5),
                  placeholder: "请输入验证码",
                  inputTextTapChange: (value) => code = value,
                  keyboardType: TextInputType.number,
                ),
              ),
              MySizeBox(width: 6),
              ShowSendCodeWidget(
                sendCodeType: SendCodeType.login,
                minWidth: 100.px,
                height: 40.px,
                bgColor: MyColors.formHex("#F8BA35FF"),
                textColor: Colors.white,
                norTextColor: Colors.white,
                sendCodeOnTap: sendCodeOnTap,
              ),
            ],
          ),
        ),
      ],
    );
  }

  saveOnTap() {
    if (!isPhoneString(newMobile)) {
      ShowToastWidget.showToast(msg: "新手机号码格式不对");
      return;
    }
    if (code == null) {
      ShowToastWidget.showToast(msg: "验证码不能为空");
      return;
    }
    if (!isCode(code!, length: 6)) {
      ShowToastWidget.showToast(msg: "验证码不对");
      return;
    }
    endKeyboard(context);

    ProgressDialog.showProgress(context);
    LoginRequest.changeMobile(code: code, phone: newMobile).then((value) {
      ProgressDialog.dismiss(context);
      if (value) {
        ShowToastWidget.showToast(msg: "修改成功");
        NavigatorUtil.pop(context);
      }
    });
  }

  Future<bool> sendCodeOnTap() async {
    if (!isPhoneString(newMobile)) {
      ShowToastWidget.showToast(msg: "新手机号码格式不对");
      return false;
    }
    return LoginRequest.sendCode(type: 16, phone: newMobile);
  }
}
